#include "Switch.h"
#include "taskLib.h"
#include "semLib.h"

#if 0
static SEM_ID mutex;

void
Switch::pollSwitches(void)
{
	while(1)
	{
		semTake(mutex, WAIT_FOREVER);
		
		std::list<Switch *>::iterator s = switches.begin();
		std::list<Position>::iterator p = last_states.begin();
		
		while(s != switches.end())
		{
			if((*p == Neutral) && ((*s)->GetPosition() != *p))
			{
				(*s)->Notify();
			}
			s++;
			p++;
		}
		semGive(mutex);
	}
}
#endif

Switch::Switch(unsigned int channel, unsigned int id)
{
#if 0
	semTake(mutex, WAIT_FOREVER);
#endif
	m_channel = channel;
	m_id      = id;
#if 0
	switches.push_back(this);
	last_states.push_back(GetPosition());
	
	static bool firstTimeHere = true;
	if(firstTimeHere)
	{
		m_taskID = taskSpawn("SwitchPoller",
				90,
				VX_FP_TASK,							// options
				20000,								// stack size
				(FUNCPTR) pollSwitches,	// function to start
				(int) this,							// parameter 1 - pointer to this class
				0, 0, 0, 0, 0, 0, 0, 0, 0);	// additional unused parameters
		if (m_taskID == ERROR)
		{
			throw -1;
		}	
		firstTimeHere = false;
	}
	semGive(mutex);
#endif
}
#if 0
void
Switch::AddListener(SwitchListener *listener)
{
	m_listeners.push_back(listener);
}

void
Switch::RemoveListener(SwitchListener *listener)
{
	m_listeners.remove(listener);
}

void
Switch::Notify(void)
{
	std::list<SwitchListener *>::iterator s = m_listeners.begin();
	
	while(s != m_listeners.end())
	{
		(*s)->SwitchChanged(m_id);
		s++;
	}
}
#endif
